﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MiniGameManager : MonoBehaviour {

	public GameObject gamePanel;
	public Button[] buttons;
	public int level;
	public int finishStatus;
	public bool inProgress;

	private static MiniGameManager instance = null;

	void Awake() {
		if (instance == null) {
			instance = this;
			Debug.Log ("MiniGameManager has initialized");
			Init ();
		} else if (instance != this) {
			Destroy(this.gameObject);
		}
	}

	public static MiniGameManager getInstance() {
		return instance;
	}

	private AdjacencyNode[] nodes;
	// Use this for initialization
	void Init () {
		setLevel (0);
		inProgress = false;
		nodes = new AdjacencyNode[9];

		// setup nodes
		nodes [0] = new AdjacencyNode (buttons [0], new AdjacencyNode (buttons [1], new AdjacencyNode (buttons [3], null)));
		nodes [1] = new AdjacencyNode (buttons [1], new AdjacencyNode (buttons [0], new AdjacencyNode (buttons [2], new AdjacencyNode(buttons[4], null))));
		nodes [2] = new AdjacencyNode (buttons [2], new AdjacencyNode (buttons [1], new AdjacencyNode (buttons [5], null)));
		nodes [3] = new AdjacencyNode (buttons [3], new AdjacencyNode (buttons [0], new AdjacencyNode (buttons [4], new AdjacencyNode(buttons[6], null))));
		nodes [4] = new AdjacencyNode (buttons [4], new AdjacencyNode (buttons [1], new AdjacencyNode (buttons [3], new AdjacencyNode(buttons[5], new AdjacencyNode(buttons[7], null)))));
		nodes [5] = new AdjacencyNode (buttons [5], new AdjacencyNode (buttons [2], new AdjacencyNode (buttons [4], new AdjacencyNode(buttons[8], null))));
		nodes [6] = new AdjacencyNode (buttons [6], new AdjacencyNode (buttons [3], new AdjacencyNode (buttons [7], null)));
		nodes [7] = new AdjacencyNode (buttons [7], new AdjacencyNode (buttons [4], new AdjacencyNode (buttons [6], new AdjacencyNode(buttons[8], null))));
		nodes [8] = new AdjacencyNode (buttons [8], new AdjacencyNode (buttons [5], new AdjacencyNode (buttons [7], null)));
	}

	public void setLevel(int newLevel) {
		Debug.Log ("Setting Level: " + newLevel);
		finishStatus = 0;
		this.level = newLevel;
		int pow = 1;
		for (int i = 0; i < 9; i++) {
			bool state = ((level & pow) != 0);
			buttons[i].GetComponent<ButtonClicked>().SetState(state);
			pow *= 2;
		}
	}

	public void ChangeLevel(int newLevel) {
		inProgress = true;
		finishStatus = 0;
		Debug.Log("MinigameManager.cs 62");
		if (Connections.ServerManager.m_deviceConnected == false) {
			Debug.Log("MinigameManager.cs 64");
			gamePanel.SetActive (true);
			setLevel (newLevel);
		}
	}

	public bool CheckGameOver() {
		if (finishStatus != 0)
			return true;

		for (int i = 0; i < 9; i++) {
			if(!buttons[i].GetComponent<ButtonClicked>().state) return false;
		}
		Debug.Log ("Game Is Over");
		inProgress = false;
		finishStatus = 1;
		gamePanel.SetActive (false);
		return true;
	}

	public void Reset() {
		setLevel (this.level);
	}

	public void Cancel() {
		Debug.Log ("Cancel is running");
		inProgress = false;
		finishStatus = -1;
		gamePanel.SetActive (false);
	}

	public void buttonClicked(int tag) {
		AdjacencyNode n = nodes [tag];
		n.click ();
		// CheckGameOver ();
	}

	private class AdjacencyNode {
		public Button b;
		public AdjacencyNode next;

		public AdjacencyNode(Button curr, AdjacencyNode nxt) {
			b = curr;
			next = nxt;
		}

		public void click() {
			b.GetComponent<ButtonClicked> ().UpdateState ();
			if(next != null) {
				next.click();
			}
		}
	}
}
